package adminYoga

import (
	"duoke/model"
	"duoke/model/yogaModel"
	"duoke/service/common"
	yogaService "duoke/service/yoga"
	"duoke/utils/gconv"
	"duoke/utils/orm"
	"duoke/utils/request"
	"duoke/utils/response"
	"duoke/utils/valid"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
)

// SchoolTimetable 按照日期划分
var SchoolTimetable = new(schoolTimetableCtl)

type schoolTimetableCtl struct{}

func (ctl *schoolTimetableCtl) Save(c *gin.Context) {
	var req yogaModel.ReqSchoolTimetable
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	err := yogaService.SchoolTimetable.Save(c, &req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "success", nil)
}
func (ctl *schoolTimetableCtl) List(c *gin.Context) {
	var req model.ReqList
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	var list []yogaModel.ReqSchoolTimetable
	orm.Db.ShowSQL(true)
	count, err := common.Tales.List(c, &req, "id desc").
		Table("yoga_school_timetable").FindAndCount(&list)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "success", gin.H{"total": count, "list": list})
}
func (ctl *schoolTimetableCtl) Info(c *gin.Context) {
	req := request.JsonMap(c)
	id := gconv.Int(req["id"])
	if id == 0 {
		response.Error(c, "参数缺失【id】", nil)
		return
	}
	where := fmt.Sprintf("id=%d", id)
	var m yogaModel.ReqSchoolTimetable
	get, err := common.Tales.Info(where, "*").Table("yoga_school_timetable").Get(&m)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	if !get {
		response.Error(c, "数据不存在", nil)
		return
	}
	response.Success(c, "success", m)
}
func (ctl *schoolTimetableCtl) Del(c *gin.Context) {
	req := request.JsonMap(c)
	id := gconv.Int(req["id"])
	if id == 0 {
		response.Error(c, "参数缺失【id】", nil)
		return
	}
	where := fmt.Sprintf("id=%d", id)
	get, err := common.Tales.Info(where, "*").Table("yoga_school_timetable").
		Delete(&yogaModel.ReqSchoolTimetable{})
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "success", get)
}
